package com.yl.wzy;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.maning.mnvideoplayerlibrary.player.YLVideoPlayer;

public class TestVideoActivity extends AppCompatActivity {

    private YLVideoPlayer mVideoPlayer;
    private String urlVideo = "http://st.chaoyindj.com/zwmtv2016/%E7%83%AD%E8%88%9EDJ%E5%97%A8%E6%9B%B2%E8%A7%86%E9%A2%91%20-%20%E7%8E%8B%E5%BB%BA%E8%8D%A3%20-%20%E5%AD%A4%E5%8D%95%E7%9A%84%E6%83%85%E4%BA%BA%E8%8A%82%E4%B8%8B%E7%9D%80%E4%BC%A4%E5%BF%83%E7%9A%84%E9%9B%A8%20-%20DJ%E4%BD%95%E9%B9%8F.mp4";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_video);
        mVideoPlayer = (YLVideoPlayer) findViewById(R.id.video_player);

        //  第一次进来先设置数据
        mVideoPlayer.setDataSource(urlVideo,"");

        //  播放完成监听
        mVideoPlayer.setOnCompletionListener(new YLVideoPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                Log.i("TestVideoActivity", "播放完成----");
            }
        });

        //  网络监听
        mVideoPlayer.setOnNetChangeListener(new YLVideoPlayer.OnNetChangeListener() {
            @Override
            public void onWifi(MediaPlayer mediaPlayer) {
            }

            @Override
            public void onMobile(MediaPlayer mediaPlayer) {
                Toast.makeText(TestVideoActivity.this, "请注意,当前网络状态切换为3G/4G网络", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNoAvailable(MediaPlayer mediaPlayer) {
                Toast.makeText(TestVideoActivity.this, "当前网络不可用,检查网络设置", Toast.LENGTH_LONG).show();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        mVideoPlayer.startVideo();
    }

    @Override
    protected void onPause() {
        super.onPause();

        mVideoPlayer.pauseVideo();
    }


    @Override
    protected void onDestroy() {
        if (mVideoPlayer != null)  //  销毁当前view
        {
            mVideoPlayer.destroyVideo();
            mVideoPlayer = null;
        }
        super.onDestroy();
    }

    public void onClick(View view) {
        mVideoPlayer.playVideo(urlVideo);
    }
}
